local public_line = {}

class.tjBtn = extends(class.button) {
    reload = true,
    icon1 = [[ui\通关存档\通关按钮-暗.tga]],
    icon2 = [[ui\通关存档\通关按钮-亮.tga]],
    new = function (parent,x,y,w,h)
        local panel = class.tjBtn:builder 
        {
            parent = parent,
            x = x,
            y = y,
            w = w,
            h = h,
        }
        return panel
    end,

    init = function (self)
        class.button.init(self)
        self:set_normal_image()
        local load_bar = self:add_panel([[textures\black32.blp]],90,self.h/2 + 10,self.w - 180,20)
        self.upgrade = self:add_button([[ReplaceableTextures\CommandButtons\BTNSkillz.blp]],self.w - 70,(self.h - 60)/2,64,64,1)
        self.upgrade.name = '升级'

        self.icon = self:add_panel([[ReplaceableTextures\CommandButtons\BTNPriest.blp]],20,10,64,64)
        self.title = self:add_text('难度1',90,0,self.w,self.h/2,12,1)
        self.title:set_color(0xff999999)

        self.load = load_bar:add_panel([[ui\expbar.tga]],0,0,nil,nil)
        self.load.max_w = self.load.w

        self.xp = load_bar:add_text('1/50',0,0,nil,nil,8,5)
        self.back = self:add_panel([[ui\通关存档\通关按钮-压暗遮罩.tga]],0,0,self.w,self.h)
        self.highlight = self:add_panel([[UI\通关存档\通关按钮-选中遮罩.tga]])
        self.highlight:hide()
        return self
    end,
    set_data = function(self,skill)
        self.skill = skill
        if skill then
            local level = skill.level or 0
            self.icon:set_normal_image(skill.art)
            self.title:set_text(skill.name)
            self.xp:set_text(('%s/50'):format(level))
            self.load:set_width((level/50) * self.load.max_w + 0.5)
            self.upgrade.skill = skill
            if level==50 then
                self:set_normal_image(self.icon2)
            else
                self:set_normal_image(self.icon1)
            end
            self:show()
        else
            self:hide()
        end
    end
}


--[面板]
class.save_ui4 = extends(class.panel) {
    reload = true,
    new = function (parent,x,y,w,h)
        local panel = class.save_ui4:builder 
        {
            parent = parent,
            x = x,
            y = y,
            w = w,
            h = h,
        }
        return panel
    end,
    init = function (self)
        class.panel.init(self)
        
        local icon = self:add_button([[ReplaceableTextures\CommandButtons\BTNSkillz.blp]],100,20,64,64)
        self.sum_button = icon
        self.sum_lv = icon:add_text('1',0,0,nil,nil,24,5)

        self.point = icon:add_text('图鉴积分：1000',icon.w + 10,0,180,icon.h/2,12,4)

        self.xpback = icon:add_panel([[textures\black32.blp]],icon.w + 10,icon.h/2 + (icon.h/2 - 15)/2,180,15)
        self.xpload = self.xpback:add_panel([[ui\expbar.tga]],0,0,nil,nil)
        self.xptext = self.xpback:add_text('0/8',0,0,nil,nil,8,5)
        self.xpload.max_w = self.xpback.w

        self.reload = self:add_button([[UI\存档装备\右侧-小标签暗.tga]],self.w - 250,10,150,50,1)
        self.reload:add_text('重置',0,0,150,50,12,5)
        self.reload:set_highlight([[UI\存档装备\右侧-小标签亮.tga]])

        --分割线
        self:add_panel([[ui\通关存档\分割线.tga]],0,60,self.w,2)
        
        local w,h = 375,80
        local buttons = {}
        for a=1,10 do
            local x = ((a-1)%2) * (w+5) + 20
            local y = (math.ceil(a/2) - 1) * (h + 5) + 110
            local button = self:add_tjBtn(x,y,w,h)
            buttons[a] = button
        end
        self.buttons = buttons
        --[翻页]
        self:add_page_area(nil,self.h-30)

        return self
    end,
        
    update = function(self)
        local player = ac.player.self
        local data = ac['图鉴熟练度'] or {}
        local sum = player:get_data('图鉴总熟练度') or 0
        local point = player:get_data('图鉴积分-剩余') or 0
        local index = (self.page - 1) * #self.buttons
        self.max_page = math.max(math.ceil(#data/#self.buttons),1)
        self.page_text:set_text(('%s/%s'):format(self.page,self.max_page))
        for a,button in ipairs(self.buttons) do
            local skill = data[a + index]
            local level = skill and skill.level or 0
            local exp = (level*10 + 100) or 0
            button:set_data(skill)
            if point<exp and level<50 then
                button.back:show()
            else
                button.back:hide()
            end
        end
        
        local lv = math.floor(sum/8)
        local xp = sum - lv * 8
        self.sum_lv:set_text(lv)
        self.point:set_text(('图鉴积分：%s'):format(point))
        self.xpload:set_width(xp/8 * self.xpload.max_w + 0.5)
        self.xptext:set_text(('%s/%s'):format(sum,(lv+1)*8))
    end,

    on_button_mouse_enter = function(self,button)
        local skill = button.skill
        if skill then
            if button.name=='升级' then
                local tip = format_obj(skill, skill.up_tip)
                button:tooltip('升级图鉴',tip,'bottom',400)
            else
                local tip = format_obj(skill, skill.tip)
                button:tooltip(skill.name,tip,'bottom',400)
            end
            --button:tooltip(skill:get_title(),skill:get_tip())
        end
        if button==self.sum_button then
            local level = ac.player.self:get_data('图鉴总熟练度') or 0
            local tip = get_sum_tips(math.floor(level/8))
            button:tooltip('图鉴系统',tip,'bottom',300)
        end
    end,
    --[点击]
    on_button_clicked = function(self,button)
        if button==self.page_add then
            self.page = math.min(self.page + 1,self.max_page)
            self:update()
        elseif button==self.page_los then
            self.page = math.max(self.page - 1,1)
            self:update()
        elseif button==self.reload then
            ac.sync('同步-图鉴重置')
        elseif button.name=='升级' then
            local skill = button.skill
            if japi.GetKeyState(KEY.ALT) then
                ac.sync('同步-图鉴升级',skill.id,50)
            else
                ac.sync('同步-图鉴升级',skill.id)
            end
        end
    end,

}

-- local w,h = 1000,600
-- local ui = class.save_ui4:builder 
-- {
--     x = (1920 - w)/2,
--     y = 150,
--     w = w,
--     h = h,
-- }